/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webaccess.retailws.Util;

import com.webaccess.retailws.Delegate.BrandDelegate;
import com.webaccess.retailws.Delegate.CategoryDelegate;
import com.webaccess.retailws.Delegate.CommonDelegate;
import java.util.LinkedHashMap;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.upload.FormFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Owner
 */
public class CommonUtil {

    CommonDelegate commDelegate = null;
    CategoryDelegate categDelegate = null;
    BrandDelegate brandDelegateObj = null;
    ActionServlet getServlet = null;

    public CommonUtil() {
        categDelegate = new CategoryDelegate();
        brandDelegateObj = new BrandDelegate();
    }

    public LinkedHashMap getStatus() {
        commDelegate = new CommonDelegate();
        LinkedHashMap statusMap = commDelegate.getStatusLHMap();
        return statusMap;
    }

    public String formatDBDate(String string) {
        String formatDBDate = null, chkDay = null, chkMonth = null, chkYear = null;
        try {
//            chkDay = chkDate.substring(0, 2) ;
//            chkMonth = chkDate.substring(3, 5);
//            chkYear = chkDate.substring(6, 10);
            formatDBDate = string.substring(8, 10) + "-" + string.substring(5, 7) + "-" + string.substring(0, 4);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return formatDBDate;
    }

    public String getCurrentDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        //get current date time with Date()
        Date date = new Date();
        return dateFormat.format(date);
    }

    String uploadFile(FormFile image, String fPath) {
        String fileName = "";
        try {
            //Get the servers upload directory real path name
            String filePath = fPath;
            //create the upload folder if not exists
            File folder = new File(filePath);
            if (!folder.exists()) {
                folder.mkdir();
            }

            fileName = image.getFileName();

            if (!("").equals(fileName)) {
                System.out.println("Server path:" + filePath);
                File newFile = new File(filePath, fileName);

                if (!newFile.exists()) {
                    try {
                        FileOutputStream fos = new FileOutputStream(newFile);
                        fos.write(image.getFileData());
                        fos.flush();
                        fos.close();
                    } catch (FileNotFoundException ex) {
                        ex.printStackTrace();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return fileName;
        }
    }

    public void getTopRightListMap(HttpServletRequest request) {
        if (request.getAttribute("categoryMap") == null || request.getAttribute("categoryMap") == "") {
            request.setAttribute("categoryMap", categDelegate.getCategoryLHMap(0));
        }
        if (request.getAttribute("brandList") == null || request.getAttribute("brandList") == "") {
            request.setAttribute("brandList", brandDelegateObj.getBrandProductList());
        }
        if (request.getAttribute("categoryList") == null || request.getAttribute("categoryList") == "") {
            request.setAttribute("categoryList", categDelegate.getMainCategoryList());
        }
    }
}
